package com.peng.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.peng.order.client.EduClient;
import com.peng.order.client.UserClient;
import com.peng.order.entity.Order;
import com.peng.order.entity.dto.CourseWebVo;
import com.peng.order.entity.dto.UcenterMember;
import com.peng.order.mapper.OrderMapper;
import com.peng.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Dengp
 * @since 2022-06-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private UserClient userClient;
    @Autowired
    private EduClient eduClient;

    @Override
    public String saveOrder(String courseId, String userId) {
        UcenterMember userInfoClient = userClient.getUserInfoClient(userId);
        CourseWebVo courseWebVo = eduClient.getCourseById(courseId);
        Order order = new Order();
        order.setOrderNo(IdWorker.getIdStr());
        order.setCourseId(courseWebVo.getId());
        order.setCourseCover(courseWebVo.getCover());
        order.setCourseTitle(courseWebVo.getTitle());
        order.setTeacherName(courseWebVo.getTeacherName());
        order.setMemberId(userId);
        order.setNickname(userInfoClient.getNickname());
        order.setMobile(userInfoClient.getMobile());
        order.setTotalFee(courseWebVo.getPrice());
        order.setStatus(0);
        order.setPayType(1);
        this.save(order);
        return order.getOrderNo();
    }
}
